define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'importschool/index/appids/'+Config.appids + location.search,
                    add_url: 'importschool/add/appids/'+Config.appids,
                    edit_url: 'importschool/edit',
                    del_url: 'importschool/del',
                    multi_url: 'importschool/multi',
                    import_url: 'importschool/import/appids/'+Config.appids,
                    table: 'importschool',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',//默认排序字段
                sortOrder:'desc',//默认排序方式
                sortName: 'id',
                toolbar: '#toolbar',
                showExport:false,
                queryParams:function(params){
                    params.filter = JSON.parse(params.filter);
                    params.op = JSON.parse(params.op);
                    params.filter.is_check = -1;
                    params.op.is_check = '=';
                    params.filter = JSON.stringify(params.filter);
                    params.op = JSON.stringify(params.op);
                    return params;
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        // {field: 'applier_id', title: __('Applier_id')},
                        {field: 'applier_name', title: __('Applier_name'),operate:false},
                        {field: 'nickname', title: __('Nickname')},
                        {field: 'card_id', title: __('Card_id'),
                            formatter: function (value,row,index) {
                                if(row.is_id_check == 2){
                                    return '<span class="text-danger">'+row.card_id+'</span>';
                                }else{
                                    return row.card_id;
                                }
                            },
                        },
                        {field: 'tel', title: __('Tel')},
                        {field: 'work_unit', title: __('Work_unit')},
                        // {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'admission_starttime', title: __('Admission_starttime'), operate:'like'},
                        {field: 'admission_endtime', title: __('Admission_endtime'), operate:'like'},
                        // {field: 'reception_id', title: __('Reception_id')},
                        {field: 'reception_name', title: __('Reception_name')},
                        {field: 'car_number', title: __('车牌号')},
                        {field: 'create_time', title: __('导入时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'is_check', title: __('Is_check')},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.buttons,width:'80px',
                            buttons:[
                                {
                                    name: 'dispatch',
                                    text: '',
                                    icon: 'fa fa-pencil',
                                    // extend: "data-area='[\"55%\", \"65%\"]'",
                                    title: '编辑',
                                    classname: 'btn btn-info btn-xs btn-dialog',
                                    visible: function (row) {
                                        return  row.is_check == -1  ? true : false;
                                    },
                                    url: 'importschool/edit',
                                },

                                
                                {
                                    name: 'dispatch',
                                    text: '',            
                                    icon: 'fa fa-trash',
                                    visible: function (row) {
                                        return  row.is_check == -1? true : false;
                                    },
                                    title: '删除',
                                    classname: 'btn btn-danger btn-xs btn-ajax',
                                    url: 'importschool/del',
                                    confirm: '确定要删除吗？',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'dispatch',
                                    text: '',
                                    icon: 'fa fa-sign-out',
                                    title: '提交审核',
                                    classname: 'btn btn-info btn-xs btn-magic btn-ajax',
                                    url: 'importschool/check',
                                    visible: function (row) {
                                        return  row.is_check == -1 ? true : false;
                                    },
                                    confirm: '确定要提交审核吗？',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },

                                {
                                    name: 'dispatch',
                                    text: '',
                                    icon: 'fa fa fa-reply-all',
                                    title: '撤回提交',
                                    classname: 'btn btn-info btn-xs btn-magic btn-ajax',
                                    url: 'importschool/back',
                                    visible: function (row) {
                                        return  row.is_check == 0  ? true : false;
                                    },
                                    confirm: '确定要撤回提交吗？',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },

                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            var table1 = $("#table1");

            // 初始化表格
            table1.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',//默认排序字段
                sortOrder:'desc',//默认排序方式
                sortName: 'id',
                toolbar: '#toolbar1',
                showExport:false,
                queryParams:function(params){
                    params.filter = JSON.parse(params.filter);
                    params.op = JSON.parse(params.op);
                    params.filter.is_check = -1;
                    params.op.is_check = '>';
                    params.filter = JSON.stringify(params.filter);
                    params.op = JSON.stringify(params.op);
                    return params;
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        // {field: 'applier_id', title: __('Applier_id')},
                        {field: 'applier_name', title: __('Applier_name'),operate:false},
                        {field: 'nickname', title: __('Nickname')},
                        {field: 'card_id', title: __('Card_id'),
                            formatter: function (value,row,index) {
                                if(row.is_id_check == 2){
                                    return '<span class="text-danger">'+row.card_id+'</span>';
                                }else{
                                    return row.card_id;
                                }
                            },
                        },
                        {field: 'tel', title: __('Tel')},
                        {field: 'work_unit', title: __('Work_unit')},
                        // {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'admission_starttime', title: __('Admission_starttime'), operate:'like'},
                        {field: 'admission_endtime', title: __('Admission_endtime'), operate:'like'},
                        // {field: 'reception_id', title: __('Reception_id')},
                        {field: 'reception_name', title: __('Reception_name')},
                        {field: 'car_number', title: __('车牌号')},
                        {field: 'create_time', title: __('导入时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'check_time', title: __('审核时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'check_cn', title: __('审核人'), operate:'false'},
                        {field: 'is_check', title: __('审核状态'), operate:false,
                            formatter: function (value,row,index) {
                                if(row.is_check==1){
                                    return '已审核';
                                }else if(row.is_check==2){
                                    return '已驳回';
                                }else{
                                    return '待审核';
                                }
                            },
                        },
			 {field: 'is_use', title: __('进校状态'), operate:false,
                            formatter: function (value,row,index) {
                                if(row.is_use==1){
                                    return '已进入';
                                }else{
                                    return '未进入';
                                }
                            },
                        },
                        {field: 'use_time', title: __('进校时间'), operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'gate_name', title: __('进校校门'), operate:false,
                            formatter: function (value,row,index) {
                                if(row.is_use==1 && !row.gate_name){
                                    return '南门';
                                }else{
                                    return row.gate_name;
                                }
                            },
                        },
                        // {field: 'is_check', title: __('Is_check')},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {field: 'operate', title: __('Operate'), table: table1, events: Table.api.events.operate, formatter: Table.api.formatter.buttons,width:'80px',
                            buttons:[
                                {
                                    name: 'dispatch',
                                    text: '',
                                    icon: 'fa fa-list',
                                    title: '进校记录',
                                    classname: 'btn btn-info btn-xs btn-magic btn-dialog',
                                    url: 'importschool/uselist',
                                    visible: function (row) {
                                        return  row.use_record_text >  1 ? true : false;
                                    },
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'dispatch',
                                    text: '',
                                    icon: 'fa fa fa-reply-all',
                                    title: '撤回提交',
                                    classname: 'btn btn-info btn-xs btn-magic btn-ajax',
                                    url: 'importschool/back',
                                    visible: function (row) {
                                        return  row.is_check ==  0 && Config.today<= row.admission_starttime ? true : false;
                                    },
                                    confirm: '确定要撤回提交吗？',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },

                            ]
                        }
                    ]
                ]

            });

            // 为表格绑定事件
            Table.api.bindevent(table1);



            //批量提交审核
            $(".btn-check").on("click", function (event) {
               var ids = Table.api.selectedids(table);
                Fast.api.ajax({
                    url: "importschool/check/ids/"+ids,
                }, function (data, ret) {
                   $(".btn-refresh").trigger("click");
                });
            });

            // // 撤销
            $(".btn-back").on("click", function (event) {
               var ids = Table.api.selectedids(table1);
                Fast.api.ajax({
                    url: "importschool/back/ids/"+ids,
                }, function (data, ret) {
                    $(".btn-refresh").trigger("click");
                });
            });

            // // 撤销
            $(".btn-recheck").on("click", function (event) {
               var ids = Table.api.selectedids(table1);
                Fast.api.ajax({
                    url: "importschool/recheck/ids/"+ids,
                }, function (data, ret) {
                    // table1.bootstrapTable('refresh');
                    $(".btn-refresh").trigger("click");
                });
            });


            //Bootstrap-table的父元素,包含table,toolbar,pagnation
            var parenttable = table.closest('.bootstrap-table');
            //Bootstrap-table配置
            var options = table.bootstrapTable('getOptions');
            //Bootstrap操作区
            var toolbar = $(options.toolbar, parenttable);
            require(['upload'], function (Upload) {
                Upload.api.plupload($('.btn-imported', toolbar), function (data, ret) {
                    console.log(data.url);
                    // Fast.api.ajax({
                    //     url: options.extend.import_url,
                    //     data: {file: data.url},
                    // }, function (data, ret) {

                    //     alert('data');
                    //     table.bootstrapTable('refresh');
                    // });
                    var index = Layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
                    $.ajax({
                        url: options.extend.import_url,
                        data: {file: data.url},
                        success:function(data){
                            Layer.closeAll('loading');
                            layer.alert(data.msg);
                           $(".btn-refresh").trigger("click");
                            // layer.alert('data.msg');
                            return;
                        },
                        error:function(){
                            Layer.closeAll('loading');
                            layer.alert(data.msg);
                            $(".btn-refresh").trigger("click");
                            return;
                        }
                    })

                });
            });


        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        check: function () {
            Controller.api.bindevent();
        },
        back: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
